<!--************************************************************************************************-->
<!-- < BookManager.php >                                                                               -->
<!-- Copyright (c) 2012-2013 San Yat-sun University School of Software                              -->
<!-- Modifier: Xiaoxin Mo                                                                 -->
<!-- Stu : 10389159                                                                                 -->
<!-- Date: 2013-01-01                                                                               -->
<!-- Destription : add function exist                                                       -->
<!--                                                                                                -->
<!-- Version : 2.1                                                                                  -->
<!--************************************************************************************************-->

<?php 
	include_once("../db/Book.php");
	
	class BookManager{
		static $instance;
		private $book;
		
		public function __construct(){
		}
		
		public static function getInstance(){
			if (self::$instance == null)
				self::$instance = new self();
			return self::$instance;
		}
		
		public function insert($array){
			$book = new Book($array);
			$book->insert();
			return true;
		}
		
		public function delete($array){
			$book = new Book($array);
			$book->delete();
		}
		
		public function update($array){
			$book = new Book($array);
			$book->update();
		}
		
		public function getValueByBookname($bookname){
			$book = new Book(null);
			return $book->findValueByBookname($bookname);
		}
		
		public function getBooklist($N){
			$book = new Book(null);
			return $book->findNBooks($N);
		}
		
		public function getBooklistByUserID($userID, $N){
			$book = new Book(null);
			
			if ($N == null)
				$book_list = $book->findBooklistByUserID($userID);
			else
				$book_list = $book->findNBooksByUserID($userID, $N);
			
			$book_names = $this->getBooknames($book_list);
			
			return $book_names;
		}
		
		public function getBooknames($book_list){
			$book_names = array();
			while($row = mysql_fetch_array($book_list))
				$book_names[] = $row['Bookname']; 
			return $book_names;
		}
		
		public function getBookImages($N){
			$book_names = $this->getBooknames($this->getBooklist($N));
			$book_imgs = array();
			
			foreach($book_names as $book_name)
				$book_imgs[] = "..\images\books\\$book_name".".jpg";
			
			return $book_imgs;
		}
		
		// ============added by xiaoxin=========
		public function exist($name){
			if ($this->getValueByBookname($name) == null)
				return false;
			return true;
		}
	}
?>